﻿Public Class util
    Private frm As Form
    Enum Etipocontrollodata  'tipo di controllo possibile per la data
        prima = 1
        dopo = 2
    End Enum

    Public Function convertData(ByVal data As Date) As String
        Dim conv As String
        Dim anno As String = data.Year
        Dim mese As String = data.Month
        Dim giorno As String = data.Day
        conv = anno & "-" & mese & "-" & giorno
        Return conv
    End Function

    Public Sub disabilitaCampi(ByVal frm As Form)
        'Dim ctrl As Control
        For i As Integer = 0 To frm.Controls.Count - 1
            If TypeOf (frm.Controls.Item(i)) Is GroupBox Then
                frm.Controls.Item(i).Enabled = False
            ElseIf TypeOf (frm.Controls.Item(i)) Is TextBox Then
                frm.Controls.Item(i).Enabled = False
            ElseIf TypeOf (frm.Controls.Item(i)) Is Button Then
                frm.Controls.Item(i).Enabled = False
            End If
        Next
    End Sub

    Public Sub puliscicampi(ByVal frm As Control)
        For i As Integer = 0 To frm.Controls.Count - 1
            If TypeOf (frm.Controls.Item(i)) Is GroupBox Then
                puliscicampi(frm.Controls.Item(i))
            End If
            If TypeOf (frm.Controls.Item(i)) Is TextBox Or (TypeOf (frm.Controls.Item(i)) Is MaskedTextBox) Then
                frm.Controls.Item(i).Text = ""
            End If
        Next
    End Sub

    Public Function controlloCampoVuoto(ByVal t As TextBox, ByVal err As ErrorProvider) As Boolean
        controlloCampoVuoto = True
        err.SetError(t, "")
        If t.Text.Trim = "" Then
            err.SetError(t, "Campo obbligatorio")
            controlloCampoVuoto = False
        End If
    End Function

    Public Function controllaData(ByVal d As MaskedTextBox, ByVal err As ErrorProvider, ByVal val As Etipocontrollodata) As Boolean

        Dim data As Date
        controllaData = Date.TryParseExact(d.Text, "dd/MM/yyyy", Application.CurrentCulture, Globalization.DateTimeStyles.NoCurrentDateDefault, data)
        If controllaData Then
            If data >= Date.Today And val = 1 Then  'es. le date di nascita devono essere minori della data corrente
                err.SetError(d, "La data inserito non è valida")
                controllaData = False
            ElseIf data < Date.Today And val = 2 Then  'es. le prenotazioni di visita devono essere maggiori o uguali a quella corrente
                controllaData = False
                err.SetError(d, "La data inserito non è valida")
            End If
        End If
    End Function

    Public Function controllaCampoNumerico(ByVal t As TextBox, ByVal err As ErrorProvider) As Boolean
        controllaCampoNumerico = True
        Dim cap As Integer
        err.SetError(t, "")
        If Not Integer.TryParse(t.Text, cap) Then
            controllaCampoNumerico = False
            err.SetError(t, "Formato non valido")
        End If
    End Function

    Public Function controlloLunghezzaCampo(ByVal t As TextBox, ByVal err As ErrorProvider, ByVal lim As Integer) As Boolean
        err.SetError(t, "")
        controlloLunghezzaCampo = True
        If Me.controlloCampoVuoto(t, err) Then
            If t.Text.Length <> lim Then
                controlloLunghezzaCampo = False
                err.SetError(t, "Lunghezza non valida")
            End If
        End If
    End Function
End Class

